﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

using Nova.CodeDOM;
using Expression = Nova.CodeDOM.Expression;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.MethodDeclBase"/>.
    /// </summary>
    public abstract class MethodDeclBaseVM : BlockStatementVM
    {
        #region /* FIELDS */

        protected ExpressionVM _explicitInterfaceExpression;
        protected ExpressionVM _returnTypeVM;
        protected ChildListVM<ParameterDeclVM> _parameterVMs;

        /// <summary>
        /// The generated WPF UI object to be highlighted for selections.
        /// </summary>
        public FrameworkElement SelectionElement;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.MethodDeclBase"/>.
        /// </summary>
        protected MethodDeclBaseVM(MethodDeclBase methodDeclBase, bool isDescription, bool createReturnTypeVM, bool createParameterVMs, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(methodDeclBase, isDescription, dictionary)
        {
            if (MethodDeclBase.IsExplicitInterfaceImplementation)
                _explicitInterfaceExpression = (ExpressionVM)CreateVM(methodDeclBase.ExplicitInterfaceExpression, isDescription, dictionary);
            if (createReturnTypeVM)
            {
                // The ReturnType of a method will be treated as TypeRef.VoidRef if null, which will cause exceptions when
                // rendering due to duplicate objects in the tree.  So, clone any such objects before rendering them.
                Expression returnType = methodDeclBase.ReturnType;
                if (returnType == TypeRef.VoidRef)
                    returnType = (Expression)TypeRef.VoidRef.Clone();
                _returnTypeVM = (ExpressionVM)CreateVM(returnType, isDescription, dictionary);
            }
            if (createParameterVMs)
                _parameterVMs = CreateListVM<ParameterDecl, ParameterDeclVM>(methodDeclBase.Parameters, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.MethodDeclBase"/> model.
        /// </summary>
        public MethodDeclBase MethodDeclBase
        {
            get { return (MethodDeclBase)CodeObject; }
        }

        /// <summary>
        /// The return type of the method (never null - will be type 'void' instead).
        /// </summary>
        public virtual ExpressionVM ReturnTypeVM
        {
            get { return _returnTypeVM; }
        }

        /// <summary>
        /// A collection of <see cref="ParameterDeclVM"/>s for the parameters of the method.
        /// </summary>
        public ChildListVM<ParameterDeclVM> ParameterVMs
        {
            get { return _parameterVMs; }
        }

        /// <summary>
        /// True if the method has parameters.
        /// </summary>
        public bool HasParameterVMs
        {
            get { return (_parameterVMs != null && _parameterVMs.Count > 0); }
        }

        /// <summary>
        /// The number of parameters the method has.
        /// </summary>
        public int ParameterVMCount
        {
            get { return (_parameterVMs != null ? _parameterVMs.Count : 0); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public static readonly Brush StaticBorderBrush = Brushes.Wheat;
        public static readonly Brush StaticBackgroundBrush = Brushes.AntiqueWhite;

        public override Brush BorderBrush
        {
            get { return StaticBorderBrush; }
        }

        public override Brush BackgroundBrush
        {
            get { return StaticBackgroundBrush; }
        }

        public override int BorderVPad
        {
            get { return 2; }
        }

        /// <summary>
        /// Determines if method parens should be hidden.
        /// </summary>
        public static bool HideMethodParens;

        protected override bool RenderParens()
        {
            return !HideMethodParens;
        }

        protected override void RenderPrefix(CodeRenderer renderer, RenderFlags flags)
        {
            renderer.RenderText(ModifiersHelpers.AsString(MethodDeclBase.Modifiers), KEYWORD_BRUSH, this);
        }

        internal virtual void RenderName(CodeRenderer renderer, RenderFlags flags)
        {
            if (MethodDeclBase.IsExplicitInterfaceImplementation)
                _explicitInterfaceExpression.Render(renderer, flags & ~(RenderFlags.Description | RenderFlags.ShowParentTypes));
            else if (!flags.HasFlag(RenderFlags.Description))
                SelectionElement = renderer.RenderText(MethodDeclBase.Name, IDENTIFIER_BRUSH, this);
            else
                renderer.RenderText(MethodDeclBase.Name, IDENTIFIER_BRUSH, this);
        }

        protected override void RenderStatement(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            if (_returnTypeVM != null)
                _returnTypeVM.Render(renderer, passFlags | RenderFlags.IsPrefix);
            if (flags.HasFlag(RenderFlags.Description))
            {
                if (ParentVM is TypeDeclVM)
                {
                    ((TypeDeclVM)ParentVM).RenderName(renderer, flags);
                    renderer.RenderText(Dot.ParseToken, IDENTIFIER_BRUSH, this);
                }
            }
            RenderName(renderer, passFlags);
        }

        protected override void RenderArgumentPrefix(CodeRenderer renderer, RenderFlags flags)
        {
            if (HideMethodParens && _parameterVMs != null && _parameterVMs.Count > 0)
                base.RenderArgumentPrefix(renderer, flags);
        }

        protected override void RenderArgument(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            RenderInfixComments(renderer, AnnotationFlags.IsInfix1, flags);
            renderer.RenderList(_parameterVMs, passFlags, this);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            renderer.RenderVisibleList(_parameterVMs, flags);
        }

        public override void UnRender()
        {
            if (_returnTypeVM != null)
                _returnTypeVM.UnRender();
            ChildListHelpers.UnRender(_parameterVMs);
            base.UnRender();
        }

        /// <summary>
        /// Get the <see cref="FrameworkElement"/> to be highlighted when the object is selected.
        /// </summary>
        public override FrameworkElement GetSelectionElement()
        {
            return (SelectionElement ?? FrameworkElement);
        }

        #endregion

        #region /* COMMANDS */

        public static readonly RoutedCommand HideMethodParensCommand = new RoutedCommand("Hide Method Parens", typeof(MethodDeclBaseVM));

        public static new void BindCommands(Window window)
        {
            WPFUtil.AddCommandBinding(window, HideMethodParensCommand, hideParens_Executed);
        }

        private static void hideParens_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            HideMethodParens = !HideMethodParens;
            CodeRenderer.ReRenderAll();
        }

        #endregion
    }
}
